package com.orm.analysis.action;

import com.opensymphony.xwork2.ActionContext;
import com.orm.analysis.model.*;
import com.orm.analysis.service.GenericService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class CustOrderAction extends FrontendAction {

    private GenericService<CustOrder> service;
    private GenericService<Address> addressService;
    private GenericService<Item> itemService;
    private GenericService<Customer> customerService;
    private GenericService<Payment> paymentService;
    private GenericService<Salesman> salesmanService;
    private CustOrder custOrder;
    private Address address;
    private Payment payment;
    private Customer customer;
    private Salesman salesman;
    private OrderDetail orderDetail;

    private List<Address>  addressList;
    private List<Customer>  customerList;
    private List<Salesman> salesmanList;
    private List<Payment> paymentList;
    private List<Item> itemList;
    private List<OrderDetail>  orderDetailList;
    private List<CustOrder>  custOrders;





    public CustOrderAction(GenericService<CustOrder> service, GenericService<Address> addressService, GenericService<Item> itemService,
                           GenericService<Customer> customerService, GenericService<Payment> paymentService, GenericService<Salesman> salesmanService) {
        this.service = service;
        this.addressService = addressService;
        this.itemService = itemService;
        this.customerService = customerService;
        this.paymentService = paymentService;
        this.salesmanService = salesmanService;
    }

    public String getAllCustOrders() {
//         custOrders = service.findAllWithDepth("orderDetails");
        custOrders = service.list(page, size);
         return SUCCESS;
    }


    public String getBulkCustOrders() {
                    try {
                        custOrders = service.findAllWithDepth("orderDetails","");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return SUCCESS;
                }

    public String getAllCustOrderCustomer() {
                    try {
                        custOrders = service.findAllWithDepth("customer");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return SUCCESS;
                }
 public String getAllCustOrderPayment() {
                    try {
                        custOrders = service.findAllWithDepth("payment");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return SUCCESS;
                }

    public String list() {
        custOrders = service.list(page, size);
        return SUCCESS;

    }

    public String setUpForInsert() {
        prep();

        return "success";
    }
    public String setUpForUpdate() {
        prep();
        if (custOrder != null && custOrder.getOrderId() != null  ) {

            custOrder = service.getById(custOrder.getOrderId());
        }
        return "success";
    }


    public String insert() {

        if (!validationSuccessful()) {
            return "input";
        } else {

            Map session = ActionContext.getContext().getSession();
            session = ActionContext.getContext().getSession();
            Object Obj = session.get("orderDetailMap");
            if (Obj != null) {

                Map<Integer, OrderDetail> orderDetailMap = (Map<Integer, OrderDetail>) Obj;
                Iterator<OrderDetail> iterator = orderDetailMap.values().iterator();
                List<OrderDetail> orderDetails = new ArrayList<OrderDetail>();
                while (iterator.hasNext()) {
                    OrderDetail orderDetailObj = iterator.next();
                    orderDetailObj.setOrderDetailId(null);
                    orderDetailObj.setCustOrder(custOrder);
                    orderDetails.add(orderDetailObj);
                }

                custOrder.setOrderDetail(orderDetails);

                if (custOrder.getOrderId() == null) {
                    service.create(custOrder);
                }

                if (custOrder.getCustomer().getId() != null) {
                    Customer  customer1 = customerService.findWithDepth("id",custOrder.getCustomer().getId(),"custOrders") ;



                    List<CustOrder> custOrderList =   customer1.getCustOrders();
                    custOrderList.add(custOrder);

                    custOrder.setCustomer(customer1);
                    customer1.setCustOrders(custOrderList);

                }

                if (custOrder.getSalesman().getId() != null) {
                    Salesman  salesman1 =  salesmanService.findWithDepth("id",custOrder.getSalesman().getId(),"custOrders");

                    List<CustOrder> custOrderList =   salesman1.getCustOrders();
                    custOrderList.add(custOrder);

                    custOrder.setSalesman(salesman1);
                    salesman1.setCustOrders(custOrderList);


                }

                if (custOrder.getPayment().getPaymentId() != null) {
                    Payment payment1 =paymentService.getById(custOrder.getPayment().getPaymentId());
                    custOrder.setPayment(payment1);
                    payment1.setCustOrder(custOrder);
                    paymentService.update(payment1);
                }

                if (custOrder.getShippingAddress().getAddressId()!= null) {
                    Address address1 =addressService.getById(custOrder.getShippingAddress().getAddressId()) ;
                    custOrder.setShippingAddress(address1);

                }


                service.update(custOrder);



            }
            return "success";
        }

    }public String update() {

        if (!validationSuccessful()) {
            return "input";
        } else {

            Map session = ActionContext.getContext().getSession();
            session = ActionContext.getContext().getSession();
            Object Obj = session.get("orderDetailMap");
            if (Obj != null) {

                Map<Integer, OrderDetail> orderDetailMap = (Map<Integer, OrderDetail>) Obj;
                Iterator<OrderDetail> iterator = orderDetailMap.values().iterator();
                List<OrderDetail> orderDetails = new ArrayList<OrderDetail>();
                while (iterator.hasNext()) {
                    OrderDetail orderDetailObj = iterator.next();
                    orderDetailObj.setCustOrder(custOrder);
                    orderDetails.add(orderDetailObj);
                }

                custOrder.setOrderDetail(orderDetails);


                if (custOrder.getOrderId() == null) {
                    service.create(custOrder);
                }

                if (custOrder.getCustomer().getId() != null) {
                    Customer  customer1 = customerService.findWithDepth("id",custOrder.getCustomer().getId(),"custOrders") ;
//                    Customer  customer1 = customerService.getById(custOrder.getCustomer().getId()) ;



                    List<CustOrder> custOrderList =   customer1.getCustOrders();
                    custOrderList.add(custOrder);

                    custOrder.setCustomer(customer1);
                    customer1.setCustOrders(custOrderList);

                }

                if (custOrder.getSalesman().getId() != null) {
                    Salesman  salesman1 =  salesmanService.findWithDepth("id",custOrder.getSalesman().getId(),"custOrders");
//                    Salesman  salesman1 =  salesmanService.getById( custOrder.getSalesman().getId());

                    List<CustOrder> custOrderList =   salesman1.getCustOrders();
                    custOrderList.add(custOrder);

                    custOrder.setSalesman(salesman1);
                    salesman1.setCustOrders(custOrderList);



                }


                if (custOrder.getPayment().getPaymentId() != null) {
                    Payment payment1 =paymentService.getById(custOrder.getPayment().getPaymentId());
                    custOrder.setPayment(payment1);
                    payment1.setCustOrder(custOrder);

                    paymentService.update(payment1);

                }

                if (custOrder.getShippingAddress().getAddressId()!= null) {
                    Address address1 =addressService.getById(custOrder.getShippingAddress().getAddressId());
                    custOrder.setShippingAddress(address1);

                }


                service.update(custOrder);




            }
            return "success";
        }

    }
    private void prep() {
        Map  session = ActionContext.getContext().getSession();
        addressList = addressService.findAllWithDepth();//list(page,size);
   		session.put("addressList", addressList);

        customerList = customerService.findAllWithDepth();//list(page,size);
        session.put("customerList", customerList);

        salesmanList = salesmanService.findAllWithDepth();//list(page,size);
        session.put("salesmanList", salesmanList);

        itemList = itemService.findAllWithDepth();//list(page,size);
        session.put("itemList", itemList);

        paymentList  = paymentService.findAllWithDepth();//list(page,size);
        session.put("paymentList", paymentList);


   	}


    public String delete() {
        service.delete(custOrder.getOrderId());
        return "success";
    }




    private boolean validationSuccessful() {

        if (custOrder.getCustomer() == null ) {
            addActionMessage("Customer is required");
        }
        if (custOrder.getRequiredDate() == null) {
            addActionMessage("Required Date is required");
        }

        if (this.hasActionMessages()) {
            return false;
        } else {
            return true;
        }

    }


    public CustOrder getCustOrder() {
        return custOrder;
    }

    public void setCustOrder(CustOrder custOrder) {
        this.custOrder = custOrder;
    }

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Salesman getSalesman() {
        return salesman;
    }

    public void setSalesman(Salesman salesman) {
        this.salesman = salesman;
    }

    public OrderDetail getOrderDetail() {
        return orderDetail;
    }

    public void setOrderDetail(OrderDetail orderDetail) {
        this.orderDetail = orderDetail;
    }

    public List<Customer> getCustomerList() {
        return customerList;
    }

    public void setCustomerList(List<Customer> customerList) {
        this.customerList = customerList;
    }

    public List<Address> getAddressList() {
        return addressList;
    }

    public void setAddressList(List<Address> addressList) {
        this.addressList = addressList;
    }

    public List<Salesman> getSalesmanList() {
        return salesmanList;
    }

    public void setSalesmanList(List<Salesman> salesmanList) {
        this.salesmanList = salesmanList;
    }

    public List<Item> getItemList() {
        return itemList;
    }

    public void setItemList(List<Item> itemList) {
        this.itemList = itemList;
    }

    public List<CustOrder> getCustOrders() {
        return custOrders;
    }

    public void setCustOrders(List<CustOrder> custOrders) {
        this.custOrders = custOrders;
    }

    public List<OrderDetail> getOrderDetailList() {
        return orderDetailList;
    }

    public void setOrderDetailList(List<OrderDetail> orderDetailList) {
        this.orderDetailList = orderDetailList;
    }
}
